package com.weizhong.update.service;import java.io.File;import android.app.DownloadManager;import android.app.Service;import android.app.DownloadManager.Request;import android.content.Context;import android.content.Intent;import android.net.Uri;import android.os.Environment;import android.os.FileObserver;import android.os.IBinder;import android.os.PowerManager;import android.text.TextUtils;import android.util.Log;import android.view.View;import com.weizhong.update.R;import com.weizhong.update.util.Constances;import com.weizhong.update.util.FileTool;import com.weizhong.update.util.Logger;import com.weizhong.update.util.UpdateDetail;
public class UpdateService extends Service {
	private static final String TAG = "UpdateService";	private UpdateDetail updateDetail;	private long fileSize;	private DownloadManager mgr;		private long lastDownload =0;		private PowerManager.WakeLock wakeLock = null; 
	@Override	public IBinder onBind(Intent intent) {		return null;	}
	@Override	public void onCreate() {		super.onCreate();
	}
	@Override	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
	}
	@Override	public int onStartCommand(Intent intent, int flags, int startId) {				Logger.i(TAG, "onstartCommand");		if(intent==null)
			return 0;		updateDetail=new UpdateDetail(this);				String downloadUrl=intent.getStringExtra("downloadUrl");		if (downloadUrl!=null){
			this.fileSize=intent.getLongExtra("fileSize", 0);
			mgr=(DownloadManager)getSystemService(DOWNLOAD_SERVICE);
			if(!TextUtils.isEmpty(downloadUrl))
				download(downloadUrl);		 	
		}else{			long downloadid = intent.getLongExtra("downloadid",-1);			if (downloadid > -1){				UpdateDownloadListener.checkAndStartup(this,downloadid); 			}		}				new MyFileObserver(Environment.getExternalStorageDirectory().getAbsolutePath()+Constances.update_dir				+ "/config.xml").startWatching();		return super.onStartCommand(intent, flags, startId);	}
	@Override	public void onLowMemory() {		super.onLowMemory();	}
	@Override	public void onDestroy() {		super.onDestroy();		stopSelf();
	}
	/**	 * 下载	 */	private void download(final String downloadUrl){		startDownload(downloadUrl);	}
    public void startDownload(String downloadUrl) { 		Log.i(TAG,"startDownload downloadUrl: " + downloadUrl);
        Uri uri=Uri.parse(downloadUrl);         String fileName=FileTool.getDownFileName(downloadUrl);        String dirPath=updateDetail.getSdcardPath()+Constances.update_dir;        try{	        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);  	        Request dwreq=new DownloadManager.Request(uri);	        Uri dpath= Uri.fromFile(new File(dirPath+"/"+fileName));                 	        dwreq.setDestinationUri(dpath);	        dwreq.setTitle(getResources().getString(R.string.notification_string)); 	        dwreq.setVisibleInDownloadsUi(false);	        dwreq.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);	        dwreq.setDescription(getResources().getString(R.string.percent_update));	        lastDownload=mgr.enqueue(dwreq,false); 			Log.i(TAG,"startDownload: lastDownload ID: "+lastDownload);	        FileTool.writer(""+lastDownload, updateDetail.getSdcardPath() + Constances.downIdLogPath, false);	        UpdateDownloadListener.checkAndStartup(this,lastDownload);         }catch(Exception e){        	e.printStackTrace();        }    }         //查看下载窗口    public void viewDownWindow(View v) {         startActivity(new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS));     }     private void releaseWack(){    	if (wakeLock != null) {              wakeLock.release();              wakeLock = null;          }      }    	class MyFileObserver extends FileObserver{		public MyFileObserver(String path) {			super(path);		}		@Override		public void onEvent(int event, String arg1) {			switch (event) {			case FileObserver.CREATE:				Log.d(TAG, "file create");				break;			case FileObserver.DELETE:				Log.d(TAG, "file delete");				break;			default:				break;			}		}	}}
